//
//  HMComposeView.swift
//  WeiBoXR
//
//  Created by acer on 16/9/30.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit
import pop
class HMComposeView: UIView {

    //  MARK: --    懒加载控件
    private lazy var sloganImageView: UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
    private lazy var buttonArray: [HMComposeButton] = [HMComposeButton]()
    //  记录当前出入的控制器
    var target: UIViewController?
    //  取到plist对象的模型数据
    private lazy var composeMenuArray: [HMComposeMenuModel] = self.loadComposePlistData()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.size = CGSize(width: ScreenWidth, height: ScreenHeight)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
//        backgroundColor = UIColor.yellow
        let image = UIImage.screenShot().applyLightEffect()
        let bgImageView = UIImageView(image: image)
        addSubview(bgImageView)
        addSubview(sloganImageView)
        
        sloganImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(100)
            make.centerX.equalTo(self)
        }
        
        addChildButton()
        
        
    }
    
    private func startPopAnimation(isUp: Bool) {
        if isUp == false {
            buttonArray = buttonArray.reversed()
        }
        
        for (i,btn) in buttonArray.enumerated() {
            let popAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
            if isUp {
                popAnimation?.toValue = NSValue(cgPoint: CGPoint(x: btn.centerX, y: btn.centerY - 350))
            } else {
                popAnimation?.toValue = NSValue(cgPoint: CGPoint(x: btn.centerX, y: btn.centerY + 350))
            }
            popAnimation?.springSpeed = 10 //初始速度
            popAnimation?.springBounciness = 10 //抖动幅度
            popAnimation?.beginTime = CACurrentMediaTime() + Double(i) * 0.025
            btn.pop_add(popAnimation, forKey: nil)
        }
        
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//        removeFromSuperview()
        startPopAnimation(isUp: false)
        self.removeFromSuperview()
    }
    
    //  添加子按钮
    private func addChildButton() {
        //  按钮的宽度
        let buttonWidth: CGFloat = 80
        //  按钮的高度
        let buttonHeight: CGFloat = 110
        //  按钮的间距
        let buttonMargin: CGFloat = (width - 3 * buttonWidth) / 4
        
        for i in 0..<composeMenuArray.count {
            //  列的索引
            let colIndex = i % 3
            //  行的索引
            let rowIndex = i / 3
            
            let composeMenuModel = composeMenuArray[i]
            let button = HMComposeButton()
            button.tag = i
            button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
            button.x = CGFloat(colIndex) * buttonWidth + CGFloat(colIndex + 1) * buttonMargin
            button.y = CGFloat(rowIndex) * buttonHeight + CGFloat(rowIndex) * buttonMargin + ScreenHeight
            
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
            button.setImage(UIImage(named: composeMenuModel.icon!), for: .normal)
            button.setTitle(composeMenuModel.title, for: .normal)
//            button.setImage(UIImage(named: "tabbar_compose_idea"), for: .normal)
//            button.setTitle("文字", for: .normal)
            
            addSubview(button)
            buttonArray.append(button)
        }
    }

    @objc private func buttonAction(btn: HMComposeButton) {
        UIView.animate(withDuration: 0.25, animations: { 
            for button in self.buttonArray {
                button.alpha = 0.2
                if button == btn {
                    button.transform = CGAffineTransform(scaleX: 2, y: 2)
                } else {
                    button.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
                }
                
            }
            }) { (_) in
                UIView.animate(withDuration: 0.25, animations: { 
                    for button in self.buttonArray {
                        button.alpha = 1
                        button.transform = CGAffineTransform.identity
                    }
                }, completion: { (_) in
                    let composeMenuModel = self.composeMenuArray[btn.tag]
                    let classType = NSClassFromString(composeMenuModel.className!) as! UIViewController.Type
                    let vc = classType.init()
                    let nav = HMNavigationController(rootViewController: vc)
                    self.target?.present(nav, animated: true, completion: {
                        self.removeFromSuperview()
                    })
                })
        }
    }
    func show(target: UIViewController)  {
        self.target = target
        target.view.addSubview(self)
        startPopAnimation(isUp: true)
    }
    
    private func loadComposePlistData() -> [HMComposeMenuModel] {
        let path = Bundle.main.path(forResource: "compose.plist", ofType: nil)!
        let dicArr = NSArray(contentsOfFile: path)!
        let modelArr = NSArray.yy_modelArray(with: HMComposeMenuModel.self, json: dicArr) as! [HMComposeMenuModel]
        return modelArr
    }
    //  读取plist文件数据
//    private func loadComposePlistData() -> [HMComposeMenuModel] {
//        //  获取plist文件的路径
//        let path = Bundle.main.path(forResource: "compose.plist", ofType: nil)!
//        //  获取到plist文件里面的数据
//        let dicArray = NSArray(contentsOfFile: path)!
//        
//        
//        let modelArray = NSArray.yy_modelArray(with: HMComposeMenuModel.self, json: dicArray)! as! [HMComposeMenuModel]
//        
//        
//        return modelArray
//    }

    
}
